from rest_framework import serializers

from Apps.Cat.models import Cat


class CatSerializer(serializers.Serializer):
    '''
    Cat 序列化类
    '''
    id = serializers.IntegerField(read_only=True)
    name = serializers.CharField(required=True, max_length=20)
    age = serializers.IntegerField(required=True)

    def create(self, validated_data):
        return Cat.objects.create(**validated_data)

    def update(self, instance, validated_data):
        instance.name = validated_data.get("name", instance.name)
        instance.age = validated_data.get("age", instance.age)
        instance.save()
        return instance